package com.java.multipledatasource.ms;

import com.alibaba.druid.sql.visitor.functions.Char;

import java.util.*;

/**
 * @author hu
 * @version 1.0
 * @date 2021/1/3 0003 下午 10:55
 * @description
 */
public class StringNumCount {


    /**
     * 3.1、输出字符串中字符和数字的个数。
     * @param str
     */
    public static void count1(String str){
        if (str != null && !"".equals(str)) {
            int numCount = 0;
            int strCount = 0;
            char[] charArray = str.toCharArray();
            for (char c : charArray) {
                if (Character.isDigit(c)) {
                    numCount++;
                } else {
                    strCount++;
                }
            }
            System.out.println("字符串中字符的个数分别为: " + numCount);
            System.out.println("字符串中数字的个数分别为: " + strCount);
        }
    }

    /**
     * 3.2、相连的数字不能分为2个，即12是作为一个数字统计，输出字符串中字符和数字的个数。
     * @param str
     */
    public static void count2(String str) {
        if (str != null && !"".equals(str)) {
            int numCount = 0;
            int strCount = 0;
            for (int i = 0; i < str.length(); i++) {
                if (Character.isDigit(str.charAt(i))) {
                    if ((i + 1) < str.length() && Character.isDigit(str.charAt(i + 1))) {
                        for (int j = i; j < str.length(); j++) {
                            if (!Character.isDigit(str.charAt(j))) {
                                numCount++;
                                i = j - 1;
                                break;
                            }
                        }
                    } else {
                        numCount++;
                    }
                } else {
                    strCount++;
                }
            }
            System.out.println("字符串中字符的个数分别为: " + numCount);
            System.out.println("字符串中数字的个数分别为: " + strCount);
        }

    }

    /**
     * 3.3、字符大小写不区分，统计字符的个数及出现次数。
     * @param str
     */
    public static void count3(String str) {
        if (str != null && !"".equals(str)) {
            String lowerStr = str.toLowerCase();
            Map<Character, Integer> countMap = new HashMap<>(lowerStr.length() * 2);
            char[] chars = lowerStr.toCharArray();
            for (char aChar : chars) {
                if (countMap.get(aChar) == null) {
                    countMap.put(aChar, 1);
                } else {
                    Integer count = countMap.get(aChar);
                    count++;
                    countMap.put(aChar, count);
                }
            }
            countMap.forEach((key, value) -> System.out.println("字符" + key + "的数量为：" + value));
        }

    }

    /**
     * 3.4、统计出现次数最多的字符和数字。
     * @param str
     */
    public static  void count4(String str) {
        if (str != null && !"".equals(str)) {
            char[] chars = str.toCharArray();
            Map<Character, Integer> charMap = new HashMap<>(str.length() * 2);
            Map<Integer, Integer> numMap = new HashMap<>(str.length() * 2);
            for (char aChar : chars) {
                if (Character.isDigit(aChar)) {
                    if (numMap.get((int) aChar - 48) != null) {
                        numMap.put((int) aChar, numMap.get((int) aChar - 48)+ 1);
                    }else {
                        numMap.put((int) aChar - 48, 1);
                    }
                } else {
                    charMap.merge(aChar, 1, Integer::sum);
                }
            }
            if (charMap.size() > 0) {
                List<Map.Entry<Character, Integer>> list = new ArrayList<>(charMap.entrySet());
                list.sort((o1, o2) -> {
                    if(o1.getValue() > o2.getValue()){
                        return 1;
                    }else if(o1.getValue().equals(o2.getValue())){
                        return 0;
                    }else{
                        return -1;
                    }
                });
                if (list.size() == 1 || !list.get(list.size() - 1).getValue().equals(list.get(list.size() - 2).getValue())) {
                    System.out.println("出现最多的字符为" + list.get(list.size() -1 ).getKey() + "出现的次数为： " + list.get(list.size() -1 ).getValue() );
                } else   {
                    StringBuilder res = new StringBuilder();
                    Integer count = list.get(list.size() -1 ).getValue();
                    for (int i = list.size() - 1; i >= 0; i--){
                        if (list.get(i).getValue().equals(count)) {
                            res.append(list.get(i).getKey()).append(",");
                        } else {
                            break;
                        }
                    }
                    System.out.println("出现最多的字符为" + res.toString() + "出现的次数为： " + count);
                }
            }


            if (numMap.size() > 0) {
                List<Map.Entry<Integer, Integer>> list = new ArrayList<>(numMap.entrySet());
                list.sort((o1, o2) -> {
                    if(o1.getValue() > o2.getValue()){
                        return 1;
                    }else if(o1.getValue().equals(o2.getValue())){
                        return 0;
                    }else{
                        return -1;
                    }
                });
                if (list.size() == 1 || !list.get(list.size() - 1).getValue().equals(list.get(list.size() - 2).getValue())) {
                    System.out.println("出现最多的数字为" + list.get(list.size() -1 ).getKey() + "出现的次数为： " + list.get(list.size() -1 ).getValue() );
                } else   {
                    StringBuilder res = new StringBuilder();
                    Integer count = list.get(list.size() -1 ).getValue();
                    for (int i = list.size() - 1; i >= 0; i--){
                        if (list.get(i).getValue().equals(count)) {
                            res.append(list.get(i).getKey()).append(",");
                        } else {
                            break;
                        }
                    }
                    System.out.println("出现最多的数字为" + res.toString() + "出现的次数为： " + count);
                }
            }
        }
    }

    public static void main(String[] args) {
        String str = "aBc12D9aB2";
        count1(str);
        count2(str);
        count3(str);
        count4(str);


    }
}
